package fr.cephb.locustree;

import java.util.regex.Pattern;

/**
 * Default URL creator
 * @author pierre
 *
 */
public class DefaultURLCreator
	implements URLCreator
	{
	private final Pattern RS_PATTERN=Pattern.compile("rs[1-9][0-9]*");
	private final Pattern ACN_PATTERN=Pattern.compile("(NM|NP)_[0-9]+");
	private final Pattern ACN_PATTERN2=Pattern.compile("[A-Z][0-9A-Z]{5,7}(\\.[0-9]+)?");
	
	
	public DefaultURLCreator()
		{
		
		}
	
	/** return a URL or null for the given parameters */
	public String createURL(
		Organism organism,
		ChromInfo chromosome,
		Track track,
		MappedObject mapped
		)
		{
		if(mapped==null || mapped.getName()==null) return null;
		if(RS_PATTERN.matcher(mapped.getName()).matches())
			{
			return "http://www.ncbi.nlm.nih.gov/projects/SNP/snp_ref.cgi?rs="+mapped.getName().substring(2);
			}
		if(ACN_PATTERN.matcher(mapped.getName()).matches())
			{
			if(mapped.getName().startsWith("NP_"))
				{
				return "http://www.ncbi.nlm.nih.gov/sites/entrez?db=protein&cmd=search&term="+mapped.getName();
				}
			else if(mapped.getName().startsWith("NM_"))
				{
				return "http://www.ncbi.nlm.nih.gov/sites/entrez?db=nuccore&cmd=search&term="+mapped.getName();
				}
			}
		if(ACN_PATTERN2.matcher(mapped.getName()).matches())
			{
			return "http://www.ncbi.nlm.nih.gov/sites/entrez?db=nuccore&cmd=search&term="+mapped.getName();
			}
		return null;
		}
	@Override
	public String toString()
		{
		return getClass().getName();
		}
	}
